package com.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Created by dell on 2016/10/26.
 */
public class AccessFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(AccessFilter.class);

    @Override
    public String filterType() {
//        pre：可以在请求被路由之前调用
//        routing：在路由请求时候被调用
//        post：在routing和error过滤器之后被调用
//        error：处理请求时发生错误时被调用
        return "pre";
    }

    @Override
    public int filterOrder() {
        //filterOrder：通过int值来定义过滤器的执行顺序
        return 1;
    }

    @Override
    public boolean shouldFilter() {
//        返回一个boolean类型来判断该过滤器是否要执行，所以通过此函数可实现过滤器的开关。
        return true;
    }

    @Override
    public Object run() {

        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
        Object accessToken = request.getParameter("accessToken");
        if(accessToken == null) {
            log.warn("access token is empty");
//            ctx.getResponse().setCharacterEncoding("utf-8");
//            try {
//                request.setCharacterEncoding("utf-8");
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            ctx.setResponseBody("you dont have auth");
            return null;
//            throw new RuntimeException("");
        }
        log.info("access token ok");
        return null;
    }
}
